package effect
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Vector3D;
	
	import kim.math.ui.Photo;

	
	/**
	 * 螺旋 
	 * @author KimWang
	 * 
	 */
	public class Helix extends MovieClip
	{
		private var num:int=50;//一共有多少个螺旋点
		private var numOfRotations:int=3;//圈数
		private var anglePer:Number = ((Math.PI*2) *numOfRotations) / num;
		private var R:int=400;//半径
		private var h:int=0;//高度
		private var contain:MovieClip=new MovieClip();//容器
		private var list:Array=new Array();
		public function Helix()
		{
			
			init();
			creat3D();
		}
		
		private function init():void
		{
			root.transform.perspectiveProjection.fieldOfView=100;//初始化透视角度
			addChild(contain);
			contain.x=stage.stageWidth/2;
			contain.y=stage.stageHeight/2-800;
			contain.z=1000;			
		}
		
		//创建螺旋效果
		private function creat3D():void
		{
			
			for (var i:int=0; i<num; i++)
			{
				var image:Photo=new Photo();
				list.push(image);
				contain.addChild(image);
				image.x=Math.cos(anglePer*i)*R;
				image.z=Math.sin(anglePer*i)*R;
				image.y=h+=30;//每一个螺旋点都递增30
				image.rotationY = (-i*anglePer) * (180/Math.PI)+90;//偏移
				image.addEventListener(Event.ENTER_FRAME,Run);
			}
		}
		
		private function Run(event:Event):void
		{
			contain.rotationY+=0.01;
			sortZ();
		}
		
		
		private function sortZ():void  
		{  
			list.sort(depthSort);  
			
			for (var i:int=0; i<list.length; i++)  
			{  
				var myimage:Photo=list[i];  
				contain.setChildIndex(myimage,i);  
				
			}  
			
		}  
		
		private function depthSort(objA:DisplayObject,objB:DisplayObject):int  
		{  
			var posA:Vector3D=objA.transform.matrix3D.position;  
			posA=contain.transform.matrix3D.deltaTransformVector(posA);  
			
			var posB:Vector3D=objB.transform.matrix3D.position;  
			posB=contain.transform.matrix3D.deltaTransformVector(posB);  
			return posB.z-posA.z;  
		}  
		
		
	}
}